<template>
    <div class="salaryPay">
        <van-nav-bar title="支付工资" left-arrow @click-left="$router.go(-1)" />
        <van-form @submit="onSubmit">
            <van-field autocomplete="off" type="number" v-model="money" name="money" label="支付总金额" readonly />
            <van-field autocomplete="off" v-model="payPwd" type="password" name="payPwd" label="支付密码" placeholder="请输入支付密码" required
                :rules="[{required: true, message: '支付密码必须输入！'}]" />

            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">
                    确定支付
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import {Toast} from 'vant'
import {Dialog} from 'vant'

export default {
  name: 'salaryPay',
  data() {
    return {
      money: 0,
      payPwd: '',
      salaryinfoidList: []
    }
  },
  components: {},

  created() {},
  mounted() {
    this.money = this.$route.query.money
    if (this.$route.query.salaryinfoidList) {
      this.salaryinfoidList = this.$route.query.salaryinfoidList.split(',')
    }
  },
  methods: {
    onSubmit() {
      let salaryListStr = ''
      for (let item of this.salaryinfoidList) {
        if (item) {
          salaryListStr = salaryListStr + ',' + item
        }
      }

      if (this.salaryinfoidList.length == 0 || salaryListStr == '') {
        Toast('请返回上一页勾选需要支付的工资信息!')
        return
      }

      let params = {
        SalaryList: salaryListStr.substring(1),
        paypassword: this.$Base64.encode(this.$md5(this.payPwd) + this.$md5('jinpin888'))
      }

      this.$api.myCompany.SalaryEnter(params).then(res => {
        if (res.code === 200) {
          let resCount = 0
          let success = 0
          let mes = ''
          for (let k in res.data) {
            resCount++
            //遍历对象，k即为key，obj[k]为当前k对应的值
            if (res.data[k].data == '支付成功') {
              success++
            } else if (mes == '') {
              mes = res.data[k].data
            }
          }

          if (success === 0) {
            Toast.success('支付失败,' + mes)
            return
          } else if (resCount === success) {
            Toast.success('支付成功')
            setTimeout(() => {
              this.$router.back()
            }, 500)
          } else if (resCount > success) {
            // Toast.success('部分工资条支付失败，请返回上一页重新勾选需要支付的工资信息!')
            Dialog.alert({
              title: '部分工资条支付失败！',
              message: '部分工资条支付失败，请返回上一页重新勾选需要支付的工资信息!',
              theme: 'round-button',
              messageAlign: 'left'
            }).then(() => {
              this.$router.back()
            })
          }
        } else {
          Toast('操作失败请重试！')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.salaryPay {
}
</style>